package com.yantong.controller;

import com.yantong.pojo.Industry;
import com.yantong.service.IndustryService;
import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 行业控制器
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 17:10
 */
@Controller
@RequestMapping("/centre/industry")
public class IndustryController extends BaseController {

	@Autowired
	private IndustryService industryService;

	private static final String prefix = "centre/industry";


	/**
	 * 页面
	 *
	 * @return
	 */
	@GetMapping("/industry")
	@RequiresPermissions("centre:industry:industry")
	public String industryBrand(Model model) {

		return prefix + "/industry";
	}


	/**
	 * 列表数据
	 *
	 * @param indutry
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("centre:industry:industry")
	@ResponseBody
	public PageUtils<Industry> listIndustry(Industry indutry, int offset, int limit) {
		indutry.setCentreId(getUserCentreId());
		return industryService.listIndustry(indutry, offset, limit);
	}

	/**
	 * 编辑页面
	 * @param industryId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{industryId}")
	@RequiresPermissions("centre:industry:edit")
	public String edit(@PathVariable Long industryId, Model model) {
		model.addAttribute("industry", industryService.getIndustryById(industryId));
		return prefix + "/edit";
	}

	/**
	 * 编辑操作
	 * @param industry
	 * @return
	 */
	@Log(module = "行业管理", operation = "编辑")
	@ResponseBody
	@RequiresPermissions("centre:industry:edit")
	@PostMapping("/update")
	public ResultVo update(Industry industry) {
		if (industry == null || industry.getId() == null) {
			return ResultVo.error();
		}
		industry.setCentreId(getUserCentreId());
		if (industryService.updateIndustry(industry) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("centre:industry:add")
	@GetMapping("/add")
	public String add(Model model) {
		return prefix +"/add";
	}


	@Log(module = "行业管理", operation = "添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("centre:industry:add")
	public ResultVo saveRate(Industry industry) {
		if (industry == null) {
			return ResultVo.error();
		}
		industry.setCentreId(getUserCentreId());
		if (industryService.saveIndustry(industry) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 移除操作
	 * @param id
	 * @return
	 */
	@Log(module = "行业管理", operation = "移除行业")
	@ResponseBody
	@RequiresPermissions("centre:industry:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return ResultVo.error();
		}
		if (industryService.removeProductGrade(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}
}
